package day02;

import template.JdbcTemplate;

import java.util.List;

public class TestJDBC4 {

    private static JdbcTemplate jdbcTemplate=new JdbcTemplate();

    public static void main(String[] args) {
//        insert();

//        update();

//        delete();

        selectById(10);

//        select();

//        selectByName("ggg");
    }



    public static void insert(){
        String sql=new StringBuffer()
                .append("insert into t_user ")
                .append("    (name) ")
                .append("values ")
                .append("    (?) ")
                .toString();
        jdbcTemplate.update(sql,"iii");
    }

    public static void update(){
        String sql=new StringBuffer()
                .append("update t_user ")
                .append("set name=? ")
                .append("where id=? ")
                .toString();
        jdbcTemplate.update(sql,"abc",6);
    }

    public static void delete(){
        String sql=new StringBuffer()
                .append("delete from t_user ")
                .append("where id=? ")
                .toString();
        jdbcTemplate.update(sql,6);
    }

    public static void select(){
        String sql=new StringBuffer()
                .append("select * ")
                .append("from t_user ")
                .toString();
        List<User> users=jdbcTemplate.query(sql,new UserRowMapper());
        for (User user : users) {
            System.out.println(user.getId()+"\t"+user.getName());
        }
    }

    public static void selectById(Integer id){
        String sql=new StringBuffer()
                .append("select * ")
                .append("from t_user ")
                .append("where id=? ")
                .toString();
        String name=jdbcTemplate.query(sql,new UserNameResultSetExtractor(),id);
        System.out.println(name);
        System.out.println("----------------------------");
        User user=jdbcTemplate.query(sql,new UserResultSetExtractor(),id);
        System.out.println(user.getId());
        System.out.println(user.getName());
    }

    public static void selectByName(String name){
        String sql=new StringBuffer()
                .append("select * ")
                .append("from t_user ")
                .append("where name=? ")
                .toString();
        List<User> users=jdbcTemplate.query(sql,new UserRowMapper(),name);
        for (User user : users) {
            System.out.println(user.getId()+"\t"+user.getName());
        }
    }





















}
